** Replication Code for:
** Terrorism Activates Ethnocentrism to Explain Greater Willingness to 
** Sacrifice Civil Liberties: Evidence from Germany
** Authors: Christina Novak Hansen and Peter Thisted Dinesen 
** Date: 10/02/2021

*-------------------------------------------------------------------------------

clear all
use GGSS2016_analysis.dta
svyset xs11 [pweight=wghtpew], vce(linearized)
keep if german==1 & governmentsplit!=.					

*-------------------------------------------------------------------------------
*Appendix P: Additional robustness checks: The activation effect over time
*-------------------------------------------------------------------------------


									
/*Rolling window regressions
To asses the effect of the activation effect over time, we generate a set of 
pre-attacks and post-attacks groups, where the "window"  - meaning the 7 consequetive 
days after the attacks - for the post-attack treatment is moved by 1 day at the
time. 

As the date variable (edate) is not continuous we generated the window groups 
manually. The first window variable, window1, is equivalent to the first
post-attack group in the treatment1 variable. This means that it consists
of those respondents who were interviewed in the first 7 days after the Würzburg
attack. The second window group consists of those who were interviewed between 
the second and eighth day after the attack. This procedure is continued until 
42 windows have been generated.

Each moving window is compared to the pre-attacks group. Thus, the category 0 in 
each dummy variable equals the pre-attacks group (those interviewed the 20 days 
before the Nice attack) and 1 equals the respective moving window. 

*/

********************************************************************************


*1: Generating windows for dummy variables
gen window1 = 1 if respid!=. & (edate>=td(15/07/2016) & edate<=td(21/07/2016))
gen window2 = 1 if respid!=. & (edate>=td(16/07/2016) & edate<=td(22/07/2016))
gen window3 = 1 if respid!=. & (edate>=td(17/07/2016) & edate<=td(23/07/2016))
gen window4 = 1 if respid!=. & (edate>=td(18/07/2016) & edate<=td(24/07/2016))
gen window5 = 1 if respid!=. & (edate>=td(19/07/2016) & edate<=td(25/07/2016))
gen window6 = 1 if respid!=. & (edate>=td(20/07/2016) & edate<=td(26/07/2016))
gen window7 = 1 if respid!=. & (edate>=td(21/07/2016) & edate<=td(27/07/2016))
gen window8 = 1 if respid!=. & (edate>=td(22/07/2016) & edate<=td(28/07/2016))
gen window9 = 1 if respid!=. & (edate>=td(23/07/2016) & edate<=td(29/07/2016))
gen window10 = 1 if respid!=. & (edate>=td(24/07/2016) & edate<=td(30/07/2016))
gen window11 = 1 if respid!=. & (edate>=td(25/07/2016) & edate<=td(31/07/2016))
gen window12 = 1 if respid!=. & (edate>=td(26/07/2016) & edate<=td(01/08/2016))
gen window13 = 1 if respid!=. & (edate>=td(27/07/2016) & edate<=td(02/08/2016))
gen window14 = 1 if respid!=. & (edate>=td(28/07/2016) & edate<=td(03/08/2016))
gen window15 = 1 if respid!=. & (edate>=td(29/07/2016) & edate<=td(04/08/2016))
gen window16 = 1 if respid!=. & (edate>=td(30/07/2016) & edate<=td(05/08/2016))
gen window17 = 1 if respid!=. & (edate>=td(31/07/2016) & edate<=td(06/08/2016))
gen window18 = 1 if respid!=. & (edate>=td(01/08/2016) & edate<=td(07/08/2016))
gen window19 = 1 if respid!=. & (edate>=td(02/08/2016) & edate<=td(08/08/2016))
gen window20 = 1 if respid!=. & (edate>=td(03/08/2016) & edate<=td(09/08/2016))
gen window21 = 1 if respid!=. & (edate>=td(04/08/2016) & edate<=td(10/08/2016))
gen window22 = 1 if respid!=. & (edate>=td(05/08/2016) & edate<=td(11/08/2016))
gen window23 = 1 if respid!=. & (edate>=td(06/08/2016) & edate<=td(12/08/2016))
gen window24 = 1 if respid!=. & (edate>=td(07/08/2016) & edate<=td(13/08/2016))
gen window25 = 1 if respid!=. & (edate>=td(08/08/2016) & edate<=td(14/08/2016))
gen window26 = 1 if respid!=. & (edate>=td(09/08/2016) & edate<=td(15/08/2016))
gen window27 = 1 if respid!=. & (edate>=td(10/08/2016) & edate<=td(16/08/2016))
gen window28 = 1 if respid!=. & (edate>=td(11/08/2016) & edate<=td(17/08/2016))
gen window29 = 1 if respid!=. & (edate>=td(12/08/2016) & edate<=td(18/08/2016))
gen window30 = 1 if respid!=. & (edate>=td(13/08/2016) & edate<=td(19/08/2016))
gen window31 = 1 if respid!=. & (edate>=td(14/08/2016) & edate<=td(20/08/2016))
gen window32 = 1 if respid!=. & (edate>=td(15/08/2016) & edate<=td(21/08/2016))
gen window33 = 1 if respid!=. & (edate>=td(16/08/2016) & edate<=td(22/08/2016))
gen window34 = 1 if respid!=. & (edate>=td(17/08/2016) & edate<=td(23/08/2016))
gen window35 = 1 if respid!=. & (edate>=td(18/08/2016) & edate<=td(24/08/2016))
gen window36 = 1 if respid!=. & (edate>=td(19/08/2016) & edate<=td(25/08/2016))
gen window37 = 1 if respid!=. & (edate>=td(20/08/2016) & edate<=td(26/08/2016))
gen window38 = 1 if respid!=. & (edate>=td(21/08/2016) & edate<=td(27/08/2016))
gen window39 = 1 if respid!=. & (edate>=td(22/08/2016) & edate<=td(28/08/2016))
gen window40 = 1 if respid!=. & (edate>=td(23/08/2016) & edate<=td(29/08/2016))
gen window41 = 1 if respid!=. & (edate>=td(24/08/2016) & edate<=td(30/08/2016))
gen window42 = 1 if respid!=. & (edate>=td(25/08/2016) & edate<=td(31/08/2016))




foreach var of varlist window* {
	gen c`var' = `var'
}

*Replacing missing values in dummy variables with 0 for observations in the pre-
*attacks group.

foreach var of varlist cwindow* {
	replace `var' = 0 if treatment1==0	
}

save GGSS2016_matched_windows.dta, replace


********************************************************************************

*2: Estimating main model for each window-dummy and storing results

/*Here, we estimate the main model (table 1, model 3) for each window variable 
using the main dependent variable - civil liberties with reference to terrorism
 - and storing the coefficient for the interaction term, the standard error, 
and the p-value in separate data files. The same process is repeated for 
authoritarianism and left-right political ideology, although these are
not displayed in the Online Appendix.*/

*Ethnocentrism
tempname e_t_C
postfile `e_t_C'  e_t_c_coef e_t_c_SE e_t_c_pval using e_t_C.dta, replace
foreach var of varlist cwindow*{
	qui svy: regress st_terrori c.`var'##c.st_ethno i.sex st_age st_age2 i.proedu2 i.work2
	matrix define temp5=r(table)
	post `e_t_C'  (`=_b[`var'#st_ethno]') (`=_se[`var'#st_ethno]') (`=temp5[4,3]')
}
postclose `e_t_C'


*Authorchild (not shown in Appendix)
tempname ac_t_C
postfile `ac_t_C'  ac_t_c_coef ac_t_c_SE ac_t_c_pval using ac_t_C.dta, replace
foreach var of varlist cwindow*{
	qui svy: regress st_terrori c.`var'##c.st_authorchild i.sex st_age st_age2 i.proedu2 i.work2
	matrix define temp5=r(table)
	post `ac_t_C'  (`=_b[`var'#st_authorchild]') (`=_se[`var'#st_authorchild]') (`=temp5[4,3]')
}
postclose `ac_t_C'


*Left-right (not shown in Appendix)
tempname lr_t_C
postfile `lr_t_C'  lr_t_c_coef lr_t_c_SE lr_t_c_pval using lr_t_C.dta, replace
foreach var of varlist cwindow*{
	qui svy: regress st_terrori c.`var'##c.st_lrID i.sex st_age st_age2 i.proedu2 i.work2
	matrix define temp5=r(table)
	post `lr_t_C'  (`=_b[`var'#st_lrID]') (`=_se[`var'#st_lrID]') (`=temp5[4,3]')
}
postclose `lr_t_C'





********************************************************************************

*3: Merging datasets into one dataset

*Merging ethnocentrism and authorchild datasets
clear
use e_t_C.dta 
gen startday =_n
save e_t_C.dta, replace

clear
use ac_t_C.dta
gen startday =_n
merge 1:1 startday using e_t_C.dta
drop _merge
save e_ac_t_C.dta, replace

*Merging with authorleaders
clear 
use lr_t_C.dta
gen startday =_n
merge 1:1 startday using e_ac_t_C.dta
drop _merge

********************************************************************************

*4: Generating variables for 95% CI's, upper and lower limits
*Ethnocentrism
gen e_c_LB = e_t_c_coef - 1.96*e_t_c_SE
gen e_c_UB = e_t_c_coef + 1.96*e_t_c_SE


*Authorchild
gen ac_c_LB = ac_t_c_coef - 1.96*ac_t_c_SE
gen ac_c_UB = ac_t_c_coef + 1.96*ac_t_c_SE


*Left-right self-placement
gen lr_c_LB = lr_t_c_coef - 1.96*lr_t_c_SE
gen lr_c_UB = lr_t_c_coef + 1.96*lr_t_c_SE

order startday ///
	e_t_c_coef e_t_c_SE e_c_LB e_c_UB e_t_c_pval ///
	ac_t_c_coef ac_t_c_SE ac_c_LB ac_c_UB ac_t_c_pval ///
	lr_t_c_coef lr_t_c_SE lr_c_LB lr_c_UB lr_t_c_pval 

save Time_figure.dta, replace




********************************************************************************

*5: Plotting the estimatet coefficients and calculated 95%-CIs


								***Figure P1***


*The activation of ethnocentrism in the first three weeks after the Normandy 
*terrorist attack

clear 
use Time_figure.dta

gen twodays=!1 if inlist(startday, 2, 4, 6 , 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42)
	
twoway (connected e_t_c_coef startday if startday<=32, mcolor(gs3) msymbol(square_hollow) lcolor(gs3)) ///
	(scatter e_t_c_coef startday if startday<=32 & e_t_c_pval<=0.05, mcolor(gs3) msymbol(square)), ///
	ytitle(Treatment effect (Interaction Term)) yline(0, lcolor(black)) ylabel(-0.4(0.2)0.8, labsize(medsmall) ///
	angle(horizontal) grid) xtitle(Days after Nice Terrorist Attack) xtitle(, size(medsmall)) ///
	xscale(range(0 30)) xscale(nofextend) xline(0 4 8 10 12, lcolor(black)) ///
	xlabel(0(2)32, labels grid) xmtick(none, labels) ///
	legend(order(1 "Ethnocentrism (insignificant)" 2 "Ethnocentrism (significant)") size(medsmall) region(lcolor(none))) xsize(7)
	
*Write city/town names for terrorist attacks manually in the graph editor.
graph export Figure_P1.pdf, replace

********************************************************************************

					***NOT SHOWN OR DESCRIBED IN ONLINE APPENDIX***

*Authoritariansim

*The activation of authoritariansism in the first three weeks after the Normandy 
*terrorist attack

twoway (connected ac_t_c_coef startday if startday<=32, mcolor(gs3) msymbol(square_hollow) lcolor(gs3)) ///
	(scatter ac_t_c_coef startday if startday<=32 & ac_t_c_pval<=0.05, mcolor(gs3) msymbol(square)), ///
	ytitle(Treatment effect (Interaction Term)) yline(0, lcolor(black)) ylabel(-0.4(0.2)0.8, labsize(medsmall) ///
	angle(horizontal) grid) xtitle(Days after Nice Terrorist Attack) xtitle(, size(medsmall)) ///
	xscale(range(0 30)) xscale(nofextend) xline(0 4 8 10 12, lcolor(black)) ///
	xlabel(0(2)32, labels grid) xmtick(none, labels) ///
	legend(order(1 "Authoritarianism (insignificant)" 2 "Authoritariansim (significant)") size(medsmall) region(lcolor(none))) xsize(7)


	
*Left-right self-placement

*The activation of Left-right self-placement in the first three weeks after the 
*Normandy terrorist attack

twoway (connected lr_t_c_coef startday if startday<=32, mcolor(gs3) msymbol(square_hollow) lcolor(gs3)) ///
	(scatter lr_t_c_coef startday if startday<=32 & lr_t_c_pval<=0.05, mcolor(gs3) msymbol(square)), ///
	ytitle(Treatment effect (Interaction Term)) yline(0, lcolor(black)) ylabel(-0.4(0.2)0.8, labsize(medsmall) ///
	angle(horizontal) grid) xtitle(Days after Nice Terrorist Attack) xtitle(, size(medsmall)) ///
	xscale(range(0 30)) xscale(nofextend) xline(0 4 8 10 12, lcolor(black)) ///
	xlabel(0(2)32, labels grid) xmtick(none, labels) ///
	legend(order(1 "Left-right self-placement (insignificant)" 2 "Left-right self-placement (significant)") size(medsmall) region(lcolor(none))) xsize(7)

